package com.ssm.util;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import com.ssm.po.User;

public class SendMessage {

    public void Success(User user) throws Exception {
        System.out.println(user.getIphone() + "++++++++++++++++++++++++++++++++++++++++++++");
        String content = "通知" + user.getName() + ":平台审核通过！";
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod("http://utf8.api.smschinese.cn");
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");// 在头文件中设置转码
        NameValuePair[] data = { new NameValuePair("Uid", "kxs123"), // 注册的用户名
                new NameValuePair("Key", "d41d8cd98f00b204e980"), // 注册成功后，登录网站后得到的密钥
                new NameValuePair("smsMob", user.getIphone()), // 手机号码
                new NameValuePair("smsText", content) };// 短信内容
        post.setRequestBody(data);

        client.executeMethod(post);
        Header[] headers = post.getResponseHeaders();
        int statusCode = post.getStatusCode();
        for (Header h : headers) {
            System.out.println(h.toString());
        }
        String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
        System.out.println(result); // 打印返回消息状态
        post.releaseConnection();
    }

    public void Fail(User user) throws Exception {
        System.out.println(user.getIphone() + "++++++++++++++++++++++++++++++++++++++++++++");
        String content = "通知" + user.getName() + ":平台审核未通过，请重新提交注册信息！";
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod("http://utf8.api.smschinese.cn");
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");// 在头文件中设置转码
        NameValuePair[] data = { new NameValuePair("Uid", "kxs123"), // 注册的用户名
                new NameValuePair("Key", "d41d8cd98f00b204e980"), // 注册成功后，登录网站后得到的密钥
                new NameValuePair("smsMob", user.getIphone()), // 手机号码
                new NameValuePair("smsText", content) };// 短信内容
        post.setRequestBody(data);

        client.executeMethod(post);
        Header[] headers = post.getResponseHeaders();
        int statusCode = post.getStatusCode();
        for (Header h : headers) {
            System.out.println(h.toString());
        }
        String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
        System.out.println(result); // 打印返回消息状态
        post.releaseConnection();
    }

}
